package com.hoow.shakoow.extend

import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Shader
import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableString
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.*
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.hoow.shakoow.R


interface OnTextViewLinkClickListener {
    fun onClick(word: String, index: Int)
}


/**增加高亮与点击事件*/
fun TextView.addLinkClickModeOnWords(
    words: Array<String>,
    listener: OnTextViewLinkClickListener?
) {
    val spanClick = SpannableString(text)
    for (index in words.withIndex()) {
        val word = index.value
        val wordindex = text.indexOf(word)
        if (wordindex != -1) {
            spanClick.setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        Log.d("TextView", "onClick: $word")
                        listener?.onClick(word, index.index)
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        super.updateDrawState(ds)
                        ds.color = ContextCompat.getColor(context, R.color.main_orange)
                        ds.isUnderlineText = true
                        ds.clearShadowLayer()
                    }
                },
                wordindex,
                wordindex + word.length,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
    }
    movementMethod = LinkMovementMethod.getInstance()
    text = spanClick
    highlightColor = ContextCompat.getColor(context, R.color.transparent)
}

/**增加副标题样式*/
fun TextView.addTipsStyle(words: String) {
    val gStart = text.indexOf(words)
    if (gStart >= 0) {
        val gEnd = gStart + words.length
        val spanString = SpannableString(text)
        spanString.setSpan(RelativeSizeSpan(0.8F), gStart, gEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        spanString.setSpan(
            ForegroundColorSpan(ContextCompat.getColor(context, R.color.black)),
            gStart,
            gEnd,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        text = spanString
    }
}

/**增加副标题样式*/
fun TextView.addTipsStyle(words: String, sizeSpan: Float, colorSpan: Int) {
    val gStart = text.indexOf(words)
    if (gStart >= 0) {
        val gEnd = gStart + words.length
        val spanString = SpannableString(text)
        spanString.setSpan(
            RelativeSizeSpan(sizeSpan),
            gStart,
            gEnd,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        spanString.setSpan(
            ForegroundColorSpan(colorSpan),
            gStart,
            gEnd,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        text = spanString
    }
}

/**增加副标题样式*/
fun TextView.addBoldStyle(words: String, colorSpan: Int) {
    val gStart = text.indexOf(words)
    if (gStart >= 0) {
        val gEnd = gStart + words.length
        val spanString = SpannableString(text)
        spanString.setSpan(
            StyleSpan(Typeface.BOLD),
            gStart,
            gEnd,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        spanString.setSpan(
            ForegroundColorSpan(colorSpan),
            gStart,
            gEnd,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        text = spanString
    }
}

/**增加副标题样式*/
fun EditText.addTipsStyle(words: String, sizeSpan: Float, colorSpan: Int) {
    val gstart = text.indexOf(words)
    val gend = gstart + words.length
    val spanString = SpannableString(text)
    spanString.setSpan(RelativeSizeSpan(sizeSpan), gstart, gend, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    spanString.setSpan(
        ForegroundColorSpan(colorSpan),
        gstart,
        gend,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    setText(spanString)
}


/**增加副标题样式*/
fun TextView.addUnderLineStyle(words: String) {
    val gstart = text.indexOf(words)
    val gend = gstart + words.length
    val spanString = SpannableString(text)
    spanString.setSpan(
        UnderlineSpan(),
        gstart,
        gend,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    setText(spanString)
}

/**增加副标题样式*/
fun TextView.addBolidStyle(words: String, colorSpan: Int) {
    val gstart = text.indexOf(words)
    val gend = gstart + words.length
    val spanString = SpannableString(text)
    spanString.setSpan(StyleSpan(Typeface.BOLD), gstart, gend, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    spanString.setSpan(
        ForegroundColorSpan(colorSpan),
        gstart,
        gend,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    text = spanString
}


/**增加高亮样式*/
fun TextView.addHighlightStyle(words: List<String>, color: Int) {
    val spanString = SpannableString(text)

    for (index in 0 until words.size) {
        val edgeString = words[index]
        val edgeFoundList = mutableListOf<Int>()
        val tempString = text.toString().trim()
        var foundIndex = -1
        do {
            val foundIndexTemp = tempString.indexOf(edgeString, foundIndex)
            if (foundIndexTemp != -1) {
                foundIndex = (foundIndexTemp + edgeString.length)
                edgeFoundList.add(foundIndexTemp)
            } else {
                foundIndex = (tempString.length + 1)
            }
        } while (foundIndex <= tempString.length)

        edgeFoundList.forEach {
            val gstart = it
            val gend = it + edgeString.length
            spanString.setSpan(
                ForegroundColorSpan(color),
                gstart,
                gend,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
    }
    text = spanString
}


/**增加高亮样式*/
fun EditText.addHighlightStyle(words: List<String>, color: Int) {
    val spanString = SpannableString(text)

    for (index in 0 until words.size) {
        val edgeString = words[index]
        val edgeFoundList = mutableListOf<Int>()
        val tempString = text.toString().trim()
        var foundIndex = -1
        do {
            val foundIndexTemp = tempString.indexOf(edgeString, foundIndex)
            if (foundIndexTemp != -1) {
                foundIndex = (foundIndexTemp + edgeString.length)
                edgeFoundList.add(foundIndexTemp)
            } else {
                foundIndex = (tempString.length + 1)
            }
        } while (foundIndex <= tempString.length)

        edgeFoundList.forEach {
            val gstart = it
            val gend = it + edgeString.length
            spanString.setSpan(
                ForegroundColorSpan(color),
                gstart,
                gend,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
    }
    setText(spanString)
}

/** 加粗 */
fun TextView.addBoldStyle() {
    typeface = Typeface.defaultFromStyle(Typeface.BOLD)
}

/** 不加粗 */
fun TextView.addNormalStyle() {
    typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
}

/** 增加下划线 */
fun TextView.addUnderLine() {
    paint.flags = Paint.UNDERLINE_TEXT_FLAG
    paint.isAntiAlias = true
}

/**
 * 设置渐变字体
 *
 * @param startColor 起始颜色
 * @param endColor 结束颜色
 */
fun TextView.addGradientStyle(startColor: Int, endColor: Int) {
    val x: Float =paint.textSize * text.length
//    val x: Float = width.toFloat()
    val y:Float = height.toFloat()
    val gradient =
        LinearGradient(0F, 0F, x, 0F, startColor, endColor, Shader.TileMode.CLAMP)
    paint.shader = gradient
    invalidate()
}
